﻿using System.Xml.Linq;
using TVersityDotNet.HelperTypes;
using TVersityDotNet.Responses;
using System.Globalization;
using System.Collections.Generic;
using System.Text;

namespace TVersityDotNet.Requests
{
    public class MediaSourceUpdateRequest : TVersityRequest
    {
        private MediaSource _mediaSource;

        public MediaSourceUpdateRequest(MediaSource mediaSource)
        {
            base.Path = "mediasource/update";
            this._mediaSource = mediaSource;
        }

        /// <summary>
        /// Returns the whole string, minus the baseurl used to process this query
        /// </summary>
        public override string RequestString
        {
            get
            {
                StringBuilder sb = new StringBuilder();
                foreach (string key in this._mediaSource.PropertyBag.PropertiesWithValues.Keys)
                {
                    sb.AppendFormat("{0}={1}&", key, this._mediaSource.PropertyBag.PropertiesWithValues[key]);
                }

                // Remove last &
                sb.Remove(sb.Length - 1, 1);
                return string.Format(CultureInfo.InvariantCulture, "{0}?{1}", this.Path, sb.ToString());
            }
        }

        public override TVersityResponse CreateResponse(XDocument xmlDocument)
        {
            MediaSourceFetchResponse response = new MediaSourceFetchResponse(xmlDocument);
            return response;
        }
    }
}
